package com.example.evensystem.viewmodel

import android.content.Context
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.evensystem.model.database.SystemDb
import com.example.evensystem.model.entity.Account
import com.example.evensystem.view.AccoutListPage
import com.example.evensystem.view.AddAccoutList
import com.example.evensystem.view.CheckPage
import com.example.evensystem.view.ChoicePage
import com.example.evensystem.view.DailyWorkPage
import com.example.evensystem.view.LoginPage
import com.example.evensystem.view.ManagerPage
import com.example.evensystem.view.MonthlyWorkPage
import com.example.evensystem.view.WorkManagePage
import com.example.evensystem.view.WorkerListPage
import com.example.evensystem.view.checkWorkPage

@Composable
fun startNavigation(
    viewModel: MainViewModel,
    context: Context,
    accoutList: SnapshotStateList<Account>
) {
    val alist=viewModel.findAllAccount()
    for (i in 0..alist.size-1){
        accoutList.add(alist.get(i))
    }
    val controller = rememberNavController()
    val onSearchDW = fun(date: String) {
        Log.d("TAG", "onSearchDW: date=$date")

        val id = viewModel.idFind.value
        if (id == null) {
            viewModel.editArrivedTimeDW("")
            viewModel.editleftTimeDW("")
            return
        }
        val dw =viewModel.findDW(id,date)
        if (dw!=null) {
            viewModel.editArrivedTimeDW(dw.arriveTime)
            viewModel.editleftTimeDW(dw.leaveTime)
        } else {
            viewModel.editArrivedTimeDW("")
            viewModel.editleftTimeDW("")
            Toast.makeText(context, "没有该日工作记录", Toast.LENGTH_SHORT).show()
        }
        Log.d(
            "TAG", "navi:上班时间：${viewModel.arrivedTimeDW.value} \" +\n" +
                    "            \"下班时间：${viewModel.leftTimeDW.value} "
        )

    }
    val onSearchMW = fun(id: String, month: String) {

        val mw = viewModel.findMW(id,month)
        if (mw!=null) {
            viewModel.editLateTimeMW(mw.lateTimes)
            viewModel.editEarlyTimeMW(mw.leftEarlyTimes)
            viewModel.editworkedDaysMW(mw.workedDates)
            viewModel.editleftDaysMW(mw.leftDates)
        } else {
            viewModel.editLateTimeMW(-1)
            viewModel.editEarlyTimeMW(-1)
            viewModel.editworkedDaysMW(-1)
            viewModel.editleftDaysMW(-1)
            Toast.makeText(context, "没有该月工作记录", Toast.LENGTH_SHORT).show()
        }
    }

    NavHost(navController = controller, startDestination = "choice") {

        composable("choice") {
            ChoicePage(modifier = Modifier, controller)
        }
        /**
         * 员工端页面
         */
        composable("login") {
            LoginPage(
                viewModel = viewModel,
                navController = controller,
                context = context
            )
        }

        composable("check") {
            CheckPage(viewModel = viewModel, navController = controller)
        }

        composable("dailyWork") {
            DailyWorkPage(onSearched = onSearchDW, viewModel = viewModel)
        }

        composable("monthlyWork") {
            MonthlyWorkPage(onSearched = onSearchMW, viewModel = viewModel)
        }

        /**
         * 管理端页面
         */
        composable("managechoice") {
            ManagerPage(navController = controller)
        }

        composable("workerlist") {
            WorkerListPage(
                list = viewModel.findAllWorker(),
                navController = controller,
                viewModel
            )
        }

        composable("checkWork") {
            checkWorkPage(
                onSearchedD = onSearchDW,
                onSearchedM = onSearchMW,
                viewModel = viewModel
            )
        }

        composable("accountlist") {
            AccoutListPage( navController = controller, list = accoutList, viewModel = viewModel)
        }

        composable("addAccount") {
            AddAccoutList(
                navController = controller,
                context = context,
                list = accoutList,
                viewModel = viewModel
            )
        }

        composable("workManage") {
            WorkManagePage(viewModel,context)
        }
    }
}